﻿using FirstFloor.ModernUI.Windows;
using FirstFloor.ModernUI.Windows.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TanmiaGrp.Basis;

namespace TanmiaGrp.GeneralLedger.WinApp.Pages
{
    /// <summary>
    /// Interaction logic for Home.xaml
    /// </summary>
    public partial class GroupEditorView : UserControl, IContent
    {
        public GroupEditorView()
        {
            InitializeComponent();
            DataContext = new GroupEditorViewModel() { Close = CloseView };
        }

        /// <summary>
        /// Called when Pop-out button is pressed, to popup the window from the docked workspace.
        /// </summary>
        private void OnPopout(object sender, RoutedEventArgs e)
        {
            // Create new EditorView and assign the same EditorViewModel object to it.
            // Hide the popup button because no need to popup an already popuped EditorView.
            GroupEditorView popupEditorView = new GroupEditorView();
            popupEditorView.DataContext = this.DataContext;
            popupEditorView.btnPopout.Visibility = System.Windows.Visibility.Collapsed;

            // Create and show the popuped window.
            new FloatWindow
            {
                Content = popupEditorView,
                ShowActivated = true,
                Owner = Application.Current.MainWindow
            }.Show();
        }

        #region IContent Members

        public void OnFragmentNavigation(FirstFloor.ModernUI.Windows.Navigation.FragmentNavigationEventArgs e) { }

        public void OnNavigatedFrom(FirstFloor.ModernUI.Windows.Navigation.NavigationEventArgs e) { }

        /// <summary>
        /// Called when current view is got the navigation.
        /// </summary>
        /// <param name="e"></param>
        public void OnNavigatedTo(FirstFloor.ModernUI.Windows.Navigation.NavigationEventArgs e)
        {
            // when browser request to edit record, CheckMessage will return true.
            if (Events.UpdateCurrentView.CheckMessage())
            {
                // Retrive the BusinessEntity that is sent from the BrowserViewModel.
                TanmiaGrp.GeneralLedger.Core.Group entity = Events.UpdateCurrentView.Receive<TanmiaGrp.GeneralLedger.Core.Group>();

                ((GroupEditorViewModel)DataContext).Entity = entity;
            }
        }

        public void OnNavigatingFrom(FirstFloor.ModernUI.Windows.Navigation.NavigatingCancelEventArgs e) { }

        #endregion

        /// <summary>
        /// Close the View that contains current EditorView.
        /// Because one ViewModel object may control many EditorViewModel objects, we have to determine where 
        /// currrent EditorView is contained to close it accordingly.
        /// </summary>
        /// <param name="sender">Represetnt the button that send Close view.</param>
        public void CloseView(object sender)
        {
            FrameworkElement senderElm = sender as FrameworkElement;

            // check if GroupEditorView is contained within FloatWindow
            FloatWindow floatWindow = senderElm.TryGetParent<FloatWindow>();
            if (floatWindow != null)
                floatWindow.Close();

            // otherwise if GroupEditorView is contained within ModernFrame that mean it is docked.
            ModernFrame modernFrame = senderElm.TryGetParent<ModernFrame>();
            if (modernFrame != null)
                modernFrame.Source = null;
        }
    }
}
